package fr.castorflex.SoundWidget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.util.Log;
import android.widget.RemoteViews;

public class SoundModeWidget extends AppWidgetProvider {

	public static String CLICK_ON_VIBRATOR = "Click on Vibrator";
	public static String CLICK_ON_MUTEMODE = "Click on Ringer";


	private static boolean normalModeMuted;
	private static boolean vibratorSoundMuted;

	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		// TODO Auto-generated method stub
		Log.i(this.getClass().getName(), "onupdate started flex");

		RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
				R.layout.main_mode);

		Intent active = new Intent(context, SoundModeWidget.class);
		active.setAction(CLICK_ON_MUTEMODE);
		PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context,
				0, active, 0);
		remoteViews.setOnClickPendingIntent(R.id.imageViewRingerMode,
				actionPendingIntent);

		active = new Intent(context, SoundModeWidget.class);
		active.setAction(CLICK_ON_VIBRATOR);
		actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
		remoteViews.setOnClickPendingIntent(R.id.imageViewVibratorMode,
				actionPendingIntent);

		init(context);
		appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
		super.onUpdate(context, appWidgetManager, appWidgetIds);

	}

	public void init(Context context) {
		AudioManager audio = (AudioManager) context
				.getSystemService(Context.AUDIO_SERVICE);

		if (audio.getVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION) != AudioManager.VIBRATE_SETTING_OFF
				|| audio.getVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER) != AudioManager.VIBRATE_SETTING_OFF) {
			vibratorSoundMuted = false;
		} else
			vibratorSoundMuted = true;

		if (audio.getRingerMode() == AudioManager.RINGER_MODE_SILENT)
			normalModeMuted = true;
		else
			normalModeMuted = false;

		updateImages(context);

	}

	public void updateImages(Context context) {
		RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
				R.layout.main_mode);

		if (normalModeMuted) {
			remoteViews.setImageViewResource(R.id.imageViewRingerMode,
					R.xml.volume_selector_off);
		} else {
			remoteViews.setImageViewResource(R.id.imageViewRingerMode,
					R.xml.volume_selector_on);
		}
		if (vibratorSoundMuted) {
			remoteViews.setImageViewResource(R.id.imageViewVibratorMode,
					R.xml.vibrator_selector_off);
		} else {
			remoteViews.setImageViewResource(R.id.imageViewVibratorMode,
					R.xml.vibrator_selector_on);
		}

		AppWidgetManager manager = AppWidgetManager.getInstance(context);
		ComponentName myWidget = new ComponentName(context, SoundModeWidget.class);
		manager.updateAppWidget(myWidget, remoteViews);
	}

	public void reloadAll(Context context, AudioManager audio) {
		
		int mode = audio.getRingerMode();
		if(mode == AudioManager.RINGER_MODE_NORMAL)
			normalModeMuted = false;
		else if(mode == AudioManager.RINGER_MODE_SILENT){
			normalModeMuted = true;
			vibratorSoundMuted = true;
		}
		else if(mode == AudioManager.RINGER_MODE_VIBRATE)
		{
			normalModeMuted = true;
			vibratorSoundMuted = false;
		}
		
		
		
		updateImages(context);
	}

	@Override
	public void onReceive(Context context, Intent intent) {

		Log.i(context.getClass().toString(), "onreceive " + intent.getAction());

		final String action = intent.getAction();

		AudioManager audio = (AudioManager) context
				.getSystemService(Context.AUDIO_SERVICE);

		if (action.equals(CLICK_ON_VIBRATOR)) {
			vibratorSoundMuted = !vibratorSoundMuted;
			if (vibratorSoundMuted) {
				audio.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER,
						AudioManager.VIBRATE_SETTING_OFF);
				audio.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION,
						AudioManager.VIBRATE_SETTING_OFF);
				if(normalModeMuted)
					audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);
			} else {
				audio.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER,
						AudioManager.VIBRATE_SETTING_ON);
				audio.setVibrateSetting(
						AudioManager.VIBRATE_TYPE_NOTIFICATION,
						AudioManager.VIBRATE_SETTING_ON);
				if (normalModeMuted) {
					audio.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
				}
			}

		} else if (action.equals(CLICK_ON_MUTEMODE)) {
			normalModeMuted = !normalModeMuted;
			if (normalModeMuted && vibratorSoundMuted)
				audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);
			else if (normalModeMuted && !vibratorSoundMuted)
				audio.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
			else
				audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
		} else{
			super.onReceive(context, intent);
			return;
		}

		updateImages(context);

		super.onReceive(context, intent);
	}

}
